﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Metadata;

namespace OptionSetBatchManager
{
    public class OptionSetData
    {
        public string EntityName { get; set; }
        public string AttributeName { get; set; }
        public bool IsCustomizable { get; set; }
        public bool IsGlobal { get; set; }
        public string GlobalOptionSetName { get; set; }
        public OptionSetMetadata OptionSetMetadata { get; set; }
        public PicklistAttributeMetadata PicklistAttributeMetadata { get; set; }
        public string FriendlyName
        {
            get
            {
                if (IsGlobal)
                {
                    return string.Format("Global Option Set:{0}", GlobalOptionSetName);
                }
                else
                {
                    return string.Format("Option Set - Entity: {0}, Attribute: {1}", EntityName, AttributeName);
                }
            }
        }

        public override string ToString()
        {
            return FriendlyName;
        }

        private OptionSetData(OptionSetMetadata optionSetMetadata)
        {
            OptionSetMetadata = optionSetMetadata;
        }

        public OptionSetData(PicklistAttributeMetadata picklistAttributeMetadata)
            :this(picklistAttributeMetadata.OptionSet)
        {
            PicklistAttributeMetadata = picklistAttributeMetadata;
            EntityName = picklistAttributeMetadata.EntityLogicalName;
            AttributeName = picklistAttributeMetadata.LogicalName;
        }

        public OptionSetData(string entityName, string attributeName, OptionSetMetadata optionSetMetadata)
            :this(optionSetMetadata)
        {
            IsCustomizable = optionSetMetadata.IsCustomizable.Value;
            IsGlobal = optionSetMetadata.IsGlobal ?? false;
        }

        public OptionSetData(string globalOptionSetName, OptionSetMetadata optionSetMetadata)
            : this(optionSetMetadata)
        {
            IsCustomizable = optionSetMetadata.IsCustomizable.Value;
            IsGlobal = optionSetMetadata.IsGlobal ?? false;
            GlobalOptionSetName = globalOptionSetName;
        }
    }
}
